package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.Agreement;
import org.hgvc.webapp.ets.bo.InstallmentSchema;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.jsf.beans.finance.MemberInvListingBean;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.hgvc.webapp.ets.spring.service.MembershipService;
import org.hgvc.webapp.ets.spring.service.SystemService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.FacesUtils;

public class AdminAgreementRescheduleBean extends AbstractBean {

	public static final String BEAN_NAME = "adminAgreementRescheduleBean";
	public static final String NAV_ACTION_BULETIN="fullpaymentFinanceRoute";
	
	private Member member;
	private Agreement agreement;
	public MembershipService membershipService;
	public MemberService memberService;
	public String installmentSchemaId;
	private List<SelectItem> selectItemSchedule;
	private SystemService systemService;
	
	public double getOutstanding(){
		double outstanding = (
				(agreement.getInstallmentSchema().getMonth() - agreement.getMemberScheduleScheme().getInstalledMonth())* (agreement.getMemberScheduleScheme().getTotalPrice()/agreement.getInstallmentSchema().getMonth()));
		return outstanding;
	}
	
	public String fullpaymentFinanceRoute(String actionNoP,String... params) throws BaseException{ 
		systemService.deleteActionBulletinByActionNo(actionNoP);
		
		MemberInvListingBean memberInvListingBean = (MemberInvListingBean)FacesUtils.getManagedBean(MemberInvListingBean.BEAN_NAME);
		memberInvListingBean.initFinanceRole();
		
		memberInvListingBean.setMemberSearch(params[2]);
		memberInvListingBean.listInvActionListener(params[1]);

		memberInvListingBean.searchMemberAction(null);
		memberInvListingBean.setActionNo(actionNoP);
		return memberInvListingBean.getInvLstAcOutcome();
	 
	}
	
	public List<SelectItem> getSelectItemSchedule() throws Exception {
		this.selectItemSchedule = new ArrayList<SelectItem>();
		List<InstallmentSchema> installmentSchemas = memberService.getInstallmentSchema();
		for (InstallmentSchema installmentSchema : installmentSchemas) {
		 
			SelectItem selectItem = new SelectItem(installmentSchema.getInstallmentSchemaId(), installmentSchema
					.getCode()
					+ " - "
					+ installmentSchema.getMonth()
					+ " Months - "
					+ installmentSchema.getInterestRate()
					+ "%"
					+ " - Principal RM "
					+ getOutstanding());
			selectItemSchedule.add(selectItem);
		}
		return selectItemSchedule;
	}

	public String fullPaymentAction() throws Exception{
		memberService.saveFullPaymentReschedule(agreement, getThisUser() );
		return AdminMemberAgreementMgmtBean.NAV_LIST.concat("_"+getRoleName());
	}
	
	public AdminAgreementRescheduleBean() {
	}

	public String submitAction() throws Exception {
		try{
			memberService.saveReshedule(agreement,  installmentSchemaId, getThisUser());
		}catch(BaseException e){
			initErrorBean(e);
			return AdminAgreementRescheduleBean.BEAN_NAME.concat("_"+getRoleName());
		}
		return AdminMemberAgreementMgmtBean.NAV_LIST.concat("_"+getRoleName());
	}

	public MembershipService getMembershipService() {
		return membershipService;
	}

	public void setMembershipService(MembershipService membershipService) {
		this.membershipService = membershipService;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public Agreement getAgreement() {
		return agreement;
	}

	public void setAgreement(Agreement agreement) {
		this.agreement = agreement;
	}

	public void iniBean(Agreement agreement) {
		this.agreement = agreement;
		this.member = agreement.getMember();
	}

	public String getInstallmentSchemaId() {
		return installmentSchemaId;
	}

	public void setInstallmentSchemaId(String installmentSchemaId) {
		this.installmentSchemaId = installmentSchemaId;
	}

	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

}
